/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.ui.playlist.GlobalItemRemoveButton');

goog.require('kramerius.audio.ui.playlist.GlobalButton');
goog.require('kramerius.audio.debug_utils');
goog.require('goog.ui.Component');
goog.require('goog.ui.Control');

/** @const */ var ITEM_REMOVE_GLOBAL_ENABLED = 'playlist-btn-item_remove-big-enabled';
/** @const */ var ITEM_REMOVE_GLOBAL_DISABLED = 'playlist-btn-item_remove-big-disabled';

/**
 * @constructor 
 * @param {Element} container Element containing this button.
 * @param {kramerius.audio.ui.playlist.Playlist} playlist containing this button.
 */
kramerius.audio.ui.playlist.GlobalItemRemoveButton = function(container, playlist){
    kramerius.audio.ui.playlist.GlobalButton.call(this, container, playlist);
    /** @private */
    //this.controller_.setPlayNextButton(this);
    /** @private */
    this.btnControl_ = this.initializeControl_();
    this.disable();
};
goog.inherits(kramerius.audio.ui.playlist.GlobalItemRemoveButton, kramerius.audio.ui.playlist.GlobalButton);


/** @private */
kramerius.audio.ui.playlist.GlobalItemRemoveButton.prototype.initializeControl_ = function(){
    var control = new goog.ui.Control();
    control.setDispatchTransitionEvents(goog.ui.Component.State.ACTION, true);
    var playlist = this.playlist_;
    var button = this;
    goog.events.listen(control, goog.ui.Component.EventType.ACTION,
        function (e) {
            switch (button.getContainerClass_()){
                case ITEM_REMOVE_GLOBAL_ENABLED:
                    //console.log("GlobalItemRemoveButton: removing");
                    var selectedItem = playlist.getSelectedItem();
                    if (selectedItem != undefined){
                        selectedItem.removeFromPlaylist();
                    }
                    else{
                        var activeItem = playlist.getActiveItem();
                        if (activeItem != undefined){
                            activeItem.moveDownInPlaylist();  
                            activeItem.removeFromPlaylist();
                        }
                    }
                    playlist.refreshGlobaButtons();
                    break;
                    break;
                case ITEM_REMOVE_GLOBAL_DISABLED:
                    //console.log("GlobalItemRemoveButton: WARNIG: clicked when inactive");
                    break;
            }
        });
    control.decorate(this.container_);
    //TODO: i18n
    new goog.ui.Tooltip(this.container_, "odstranit ze seznamu skladeb");
    return control;
};


kramerius.audio.ui.playlist.GlobalItemRemoveButton.prototype.disable = function(){
    this.setContainerClass_(ITEM_REMOVE_GLOBAL_DISABLED);
};


kramerius.audio.ui.playlist.GlobalItemRemoveButton.prototype.enable = function(){
    this.setContainerClass_(ITEM_REMOVE_GLOBAL_ENABLED);
};
